//
//  SpecificTrainingViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/29.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "SpecificTrainingViewController.h"
#import "TreeTableView.h"
#import "PlayVideoViewController.h"

@interface SpecificTrainingViewController ()<TreeTableViewDelegate>

@property (nonatomic, strong) SpecificTrainingViewModel * viewModel;
@property (nonatomic, strong) TreeTableView * treeTableView;
@end

@implementation SpecificTrainingViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    self.view.backgroundColor = [UIColor whiteColor];
    if (!self.viewModel.dataSource) {
        [self.viewModel.getTrainingProjectCommand execute:nil];
    }
}

- (void)bindViewModel
{
    @weakify(self);
    [RACObserve(self.viewModel, trainingPart) subscribeNext:^(NSNumber * x) {
        @strongify(self);
        NSString * title = [NSString string];
        switch ([x integerValue]) {
            case 0:
                title = @"颈部锻炼项目";
                break;
            case 1:
                title = @"腰部锻炼项目";
                break;
            case 2:
                title = @"足膝锻炼项目";
                break;
                
            default:
                break;
        }
        self.title = title;
    }];
    [[RACObserve(self.viewModel, dataSource) skip:1] subscribeNext:^(NSMutableArray * x) {
        @strongify(self);
        if (x.count > 0) {
            [self.view addSubview:self.treeTableView];
            [self.treeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(@0);
            }];
        }
    }];
}

- (void)makeConstaints
{
    
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if ([self.treeTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.treeTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.treeTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.treeTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - TreeTableViewDelegate
- (void)tableViewDidSelectWithNode:(Node *)node
{
    PlayVideoViewModel * viewModel = [[PlayVideoViewModel alloc] init];
    viewModel.targetNode = node;
    PlayVideoViewController * vc = [[PlayVideoViewController alloc] initWithViewModel:viewModel];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter
- (TreeTableView *)treeTableView
{
    if (!_treeTableView) {
        if (self.viewModel.dataSource.count > 0) {
            _treeTableView = [[TreeTableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)
                                                         withData:self.viewModel.dataSource];
            _treeTableView.treeTableViewDelegate = self;
        }
    }
    return _treeTableView;
}

#pragma mark - dealloc
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
